package com.siashan.toolkit.image.painter;

import com.siashan.toolkit.image.element.AbstractElement;
import com.siashan.toolkit.image.element.ImageElement;
import com.siashan.toolkit.image.element.RectangleElement;
import com.siashan.toolkit.image.element.TextElement;

/**
 * 绘制器工厂类
 *
 * @author  siashan
 * @since   1.0.8
 */
public class PainterFactory {
    /**
     * 图片绘制器
     */
    private static ImagePainter imagePainter;
    /**
     * 文本绘制器
     */
    private static TextPainter textPainter;
    /**
     * 矩形绘制器
     */
    private static RectanglePainter rectanglePainter;

    /**
     * 创建绘制器实例
     *
     * @param element    元素
     * @return           实例
     * @throws Exception 异常
     */
    public static IPainter createInstance(AbstractElement element) throws Exception {

        //考虑到性能，这里用单件，先不lock了
        if (element instanceof ImageElement) {
            if (imagePainter == null) {
                imagePainter = new ImagePainter();
            }
            return imagePainter;
        } else if (element instanceof TextElement) {
            if (textPainter == null) {
                textPainter = new TextPainter();
            }
            return textPainter;
        } else if (element instanceof RectangleElement) {
            if (rectanglePainter == null) {
                rectanglePainter = new RectanglePainter();
            }
            return rectanglePainter;
        } else {
            throw new Exception("不支持的Painter类型");
        }
    }
}
